<template>
    <div class="detail top-page" ref="detailRef">
        <tab-control v-if="showTabContral" class="tabs" :titles="names" @tabItemClick="tabClick" ref="tabControlRef" />
        <van-nav-bar title="房屋详情" left-text="旅途" left-arrow @click-left="onClickLeft" />
        <div class="main" v-if="mainPart" v-memo="[mainPart]">
            <detail-swipe :swipe-data="mainPart.topModule.housePicture.housePics" />
            <detail-info name="描述" :ref="getSectionRef" :top-infos="mainPart.topModule" />
            <detail-facility name="设施" :ref="getSectionRef"
                :house-facility="mainPart.dynamicModule.facilityModule.houseFacility" />
            <detail-landlord name="房东" :ref="getSectionRef" :landlord="mainPart.dynamicModule.landlordModule" />
            <detail-comment name="评论" :ref="getSectionRef" :comment="mainPart.dynamicModule.commentModule" />
            <detail-notice name="须知" :ref="getSectionRef"
                :order-rules="mainPart.dynamicModule.rulesModule.orderRules" />
            <detail-map name="周边" :ref="getSectionRef" :position="mainPart.dynamicModule.positionModule" />
            <detail-intro :price-intro="mainPart.introductionModule" />
        </div>
        <div class="footer">
            <img src="@/assets/img/detail/icon_ensure.png" alt="">
            <div class="text">探索未知，启程未来。</div>
        </div>
    </div>
</template>


<script setup>
import detailSwipe from './cpns/detail-swipe.vue';
import detailInfo from './cpns/detail-infos.vue';
import detailFacility from './cpns/detail-facility.vue';
import detailLandlord from './cpns/detail-landlord.vue';
import detailComment from './cpns/detail-comment.vue';
import detailNotice from './cpns/detail-notice.vue';
import detailMap from './cpns/detail-map.vue';
import detailIntro from './cpns/detail-intro.vue';
import tabControl from '@/components/tab-control/tab-control.vue';

import { useRoute, useRouter } from 'vue-router';
import { getDetailInfos } from "@/services"
import { ref, computed, watch } from 'vue';
import useScroll from '@/hooks/useScroll';


const router = useRouter()
const route = useRoute()
const houseId = route.params.id

// 发送网络请求
const detailInfos = ref({})
const mainPart = computed(() => detailInfos.value.mainPart)
getDetailInfos(houseId).then(res => {
    detailInfos.value = res.data
})


// 监听返回按钮的点击
const onClickLeft = () => {
    router.back()
}

// tabControl
const detailRef = ref()
const { scrollTop } = useScroll(detailRef)
const showTabContral = computed(() => {
    return scrollTop.value > 300
})

const sectionEls = ref({})
const names = computed(() => {
    return Object.keys(sectionEls.value)
})
const getSectionRef = (value) => {
    const name = value?.$el?.getAttribute("name")
    sectionEls.value[name] = value?.$el
}
// tab 点击事件
let isClick = false
let currentDistance = -1
const tabClick = (index) => {
    const key = Object.keys(sectionEls.value)[index]
    const el = sectionEls.value[key]
    let distance = el.offsetTop
    if (index !== 0) {
        distance = distance - 44
    }

    isClick = true
    currentDistance = distance

    detailRef.value.scrollTo({
        top: distance,
        behavior: "smooth"
    })
}

// 监听页面滚动，自动匹配tab
const tabControlRef = ref()
watch(scrollTop, (newValue) => {
    if (newValue === currentDistance) {
        isClick = false
    }
    if (isClick) return
    // 获取所有区域的offsetTop
    const els = Object.values(sectionEls.value)
    const values = els.map(el => el.offsetTop)
    // 匹配需要的索引
    let index = values.length - 1
    for (let i = 0; i < values.length; i++) {
        if (values[i] > newValue + 44) {
            index = i - 1
            break
        }
    }
    tabControlRef.value?.setCurrentIndex(index)
})

</script>


<style lang="less" scoped>
.tabs {
    position: fixed;
    background-color: #fff;
    z-index: 99;
    left: 0;
    top: 0;
    right: 0;
}

.footer {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 120px;

    img {
        width: 123px;
    }

    .text {
        font-size: 12px;
        color: #7688a7;
        margin-top: 12px;
        margin-left: 13px;
    }
}
</style>